package com.example.wanbaqian.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.wanbaqian.R;
import com.example.wanbaqian.base.BaseActivity;
import com.example.wanbaqian.config.CommonValue;
import com.example.wanbaqian.base.HttpCallback;
import com.example.wanbaqian.config.HttpURL;
import com.example.wanbaqian.bean.BackMoneyBean;
import com.example.wanbaqian.bean.BackMoneyDetailsBean;
import com.example.wanbaqian.bean.Info;
import com.example.wanbaqian.utils.GetJsonUtils;
import com.example.wanbaqian.utils.HttpUtils;
import com.example.wanbaqian.utils.JsonUtils;
import com.example.wanbaqian.utils.SPUtils;
import com.zhy.autolayout.AutoRelativeLayout;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/4/13.
 * 最近还款信息
 */

public class BackMoneyActivity extends BaseActivity implements HttpCallback {
    private static final String TAG = "BackMoneyActivity";
    @BindView(R.id.btn_ok)
    Button btn_ok;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_day)
    TextView tvDay;
    @BindView(R.id.tv_money2)
    TextView tvMoney2;
    @BindView(R.id.tv_money3)
    TextView tvMoney3;
    @BindView(R.id.tv_hint)
    TextView tv_hint;
    @BindView(R.id.rl_info)
    AutoRelativeLayout rl_info;
    private BackMoneyBean bean;

    @Override
    protected int getLayout() {
        return R.layout.activity_backmoney;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setStatusBarColor(R.color.main_color);
        setToolBar("具体订单还款");
//        toolBar_tvRight.setText("借款记录");
//        toolBar_tvRight.setVisibility(View.VISIBLE);
//        toolBar_tvRight.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                openActivity(RepaymentListActivity.class);
//            }
//        });
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initDate() {
    }

    @Override
    protected void onResume() {
        super.onResume();
        HttpUtils.getInstance().post(GetJsonUtils.getInstance().getRequest(HttpURL.REPAY_LATEST, mContext,
                new String[]{"accessid"},
                new String[]{SPUtils.getAccessid(mContext)}), mContext, null, CommonValue.REQUEST_CODE1, this);

    }

    /**
     * 设置当前界面是否有还款的界面
     *
     * @param isReturn 是否全部还完
     */
    private void setVisibility(boolean isReturn) {
        if (!isReturn) {
            //本期应还本金+本期应还服务费+本期应还滞纳金
            tvMoney.setText((Double.parseDouble(bean.getLoan_capital()) + Double.parseDouble(bean.getLoan_interest()) + Double.parseDouble(bean.getLoan_latefee())) + "");
            tvMoney2.setText(bean.getLoan_capital());
            tvDay.setText(bean.getLoan_days());
            tvMoney3.setText(bean.getLoan_money());
            tv_hint.setTextSize(12);
            tv_hint.setText("按日计息，可提前还清，随用随借");
            btn_ok.setText("立即还款");
            rl_info.setVisibility(View.VISIBLE);
        } else {
            rl_info.setVisibility(View.GONE);
            tv_hint.setTextSize(16);
            tv_hint.setText("你没有还款信息");
            tv_hint.setPadding(0, 0, 0, 100);
            btn_ok.setText("立即借款");
        }
    }


    @OnClick({R.id.return_money_layout, R.id.btn_ok})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.return_money_layout:
                Bundle bundle = new Bundle();
                bundle.putSerializable("loan_no", bean.getLoan_no());
                openActivity(RepaymentListByStageActivity.class, bundle);
                break;
            case R.id.btn_ok:
                if (btn_ok.getText().equals("立即还款")) {
                    HttpUtils.getInstance().post(GetJsonUtils.getInstance().getRequest(HttpURL.REPAY_DETAIL, mContext,
                            new String[]{"accessid", "repay_no"},
                            new String[]{SPUtils.getAccessid(mContext), bean.getRepay_no()}), mContext, null, CommonValue.REQUEST_CODE2, this);
                } else
                    finish();
                break;
        }
    }

    @Override
    public void onSuccess(String content, Info info, int requestCode) {
//        Log.i(TAG, "onSuccess: " + content);
        switch (requestCode) {
            case CommonValue.REQUEST_CODE1:
                bean = JsonUtils.getInstance().jsonToObjectk(content, BackMoneyBean.class);
//                bean = new BackMoneyBean("2017-4-24 15:57:27", "第一期", "10", "2000.00", "200.00", "0.00", "2017-4-24 15:58:23", "0", "0", "0", "20", "6000.00");
                //为空时说明没有还款信息
                setVisibility(TextUtils.isEmpty(content));
                break;
            case CommonValue.REQUEST_CODE2:
                Bundle bundle = new Bundle();
                bundle.putSerializable("backMoneyBean", JsonUtils.getInstance().jsonToObjectk(content, BackMoneyDetailsBean.class));
                openActivity(BackMoneyOneActivity.class, bundle);
                break;
        }
    }

    @Override
    public void onCacheSuccess(String content, Info info, int requestCode) {

    }
    @Override
    public void onError(Info info, int requestCode) {
        showToast(info.getMsg());
//        Bundle bundle = new Bundle();
//        bundle.putSerializable("backMoneyBean", new BackMoneyDetailsBean("第一期","10","2017-4-24 16:09:23","2000.00","200.00","0.00","0"));
//        openActivity(BackMoneyOneActivity.class, bundle);
    }
}
